package com.buddy.algorithm.list;

import java.util.Scanner;

/**
 *
 */
public class SequentialList {

    class DATA {
        String key;
        String name;
        int age;
    }

    class SLType {
        static final int MAXLEN = 100;
        DATA[] ListData = new DATA[MAXLEN + 1];
        int ListLen;

        void SLInit(SLType SL) {
            SL.ListLen = 0;
        }

        int SLLength(SLType SL) {
            return SL.ListLen;
        }

        int SLInsert(SLType SL, int n, DATA data) {
            int i;
            if (SL.ListLen >= MAXLEN) {
                System.out.println("SL is full,can not insert!");
                return 0;
            }
            if (n < 1 || n > SL.ListLen - 1) {
                System.out.println("The index n is wrong, can not insert!");
                return 0;
            }
            for (i = SL.ListLen; i >= n; i--) {
                SL.ListData[i + 1] = SL.ListData[i];
            }
            SL.ListData[n] = data;
            SL.ListLen++;
            return 1;
        }

        int SLAdd(SLType SL, DATA data) {
            if (SL.ListLen > MAXLEN) {
                System.out.println("SL is full,can not add!");
                return 0;
            }
            SL.ListData[++SL.ListLen] = data;
            return 1;
        }

        int SLDelete(SLType SL, int n) {
            int i;
            if (n < 1 || n > SL.ListLen + 1) {
                System.out.println("The index n is wrong, can not delete!");
                return 0;
            }
            for (i = n; i < SL.ListLen; i++) {
                SL.ListData[i] = SL.ListData[i + 1];
            }
            SL.ListLen--;
            return 1;
        }

        DATA SLFindByNum(SLType SL, int n) {
            if (n < 1 || n > SL.ListLen + 1) {
                System.out.println("The index n is wrong, can not find!");
                return null;
            }
            return SL.ListData[n];
        }

        int SLFindByCount(SLType SL, String key) {
            int i;
            for (i = 1; i <= SL.ListLen; i++) {
                if (SL.ListData[i].key.compareTo(key) == 0) {
                    return i;
                }
            }
            return 0;
        }

        int SLAll(SLType SL) {
            int i;
            for (i = 1; i <= SL.ListLen; i++) {
                System.out.printf("(%s,%s,%d) \n", SL.ListData[i].key, SL.ListData[i].name, SL.ListData[i].age);
            }
            return 0;
        }

    }


    public static void main(String[] args) {
        int i;
        SLType SL = new SequentialList().new SLType();
        DATA pdata;
        String key;
        System.out.println("-----------> start ......");

        SL.SLInit(SL);
        System.out.println("-----------> init finish!");

        Scanner input = new Scanner(System.in);
        do {
            System.out.println("input key,name,age:");
            DATA data = new SequentialList().new DATA();
            data.key = input.next();
            data.name = input.next();
            data.age = input.nextInt();
            if (data.age != 0){
                if (SL.SLAdd(SL,data) ==0){
                    break;
                }
            }else {
                break;
            }
        }while (true);
        System.out.println("sequential list:");
        SL.SLAll(SL);

        System.out.println("index:");
        i = input.nextInt();
        pdata = SL.SLFindByNum(SL,i);
        if (pdata != null){
            System.out.printf("index=%d,data=%s %s %d \n",i,pdata.key,pdata.name,pdata.age);
        }

        System.out.println("key:");
        key = input.next();
        i = SL.SLFindByCount(SL,key);
        pdata = SL.SLFindByNum(SL,i);
        if (pdata != null){
            System.out.printf("index=%d,data=%s %s %d \n",i,pdata.key,pdata.name,pdata.age);
        }
    }

}
